<%@ page import="org.springframework.security.core.context.SecurityContextHolder" %>
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="utf8" language="java" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<header>
    <div class="form-group media pull-right">
        <c:set var="userName"
               value="<%=SecurityContextHolder.getContext().getAuthentication().getName()%>"/>
        <h3>Hello
            <a href="<c:url value="/index"/>">
                ${userName}
            </a>
        </h3>

        <form action="<c:url value="/j_spring_security_logout"/>" method="POST" id="logoutForm">
            <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
        </form>
        <script>
            function formSubmit() {
                document.getElementById("logoutForm").submit();
            }
        </script>
        <div class="btn-group btn-group-sm pull-right">
            <c:choose>
                <c:when test="${userName ne 'guest'}">
                    <button onclick="javascript:formSubmit()" type="button" class="btn btn-link">Logout</button>
                </c:when>
                <c:otherwise>
                    <button onclick="window.location='<c:url value='/login'/>'" type="button" class="btn btn-link">Login
                    </button>
                    <button onclick="window.location='<c:url value='/register'/>'" type="button" class="btn btn-link">
                        Register
                    </button>
                </c:otherwise>
            </c:choose>
        </div>
    </div>
</header>